// 第二
$.ajax({
    type: "post",
    async: true, //异步执行
    url: "nydsj/sdcl.html",
    dataType: "json", //返回数据形式为json
    success: function (json) {
        var myChart = echarts.init(document.getElementById('main2'));
        option = {
            // backgroundColor: '#00265f',
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'shadow'
                }
            },
            legend: {
                data: ['2016', '2017', '2018'],
                align: 'right',
                right: 10,
                textStyle: {
                    color: "#fff"
                },
                itemWidth: 10,
                itemHeight: 10,
                itemGap: 35
            },
            grid: {

                x:'0%',
                x2:'5%',

                y:'10%',
                y2:'20%',
                containLabel: true
            },
            xAxis: [{
                type: 'category',
                data: [],
                axisLine: {
                    show: true,
                    lineStyle: {
                        color: "#063374",
                        width: 1,
                        type: "solid"
                    }
                },
                axisTick: {
                    show: false,
                },
                axisLabel: {
                    show: true,
                    textStyle: {
                        color: "#00c7ff",
                    }
                },
            }
            ],
            yAxis: [{
                type: 'value',
                axisLabel: {
                    formatter: '{value} %'
                },
                axisTick: {
                    show: false,
                },
                axisLine: {
                    show: false,
                    lineStyle: {
                        color: "#00c7ff",
                        width: 1,
                        type: "solid"
                    },
                },
                splitLine: {
                    lineStyle: {
                        color: "#063374",
                    }
                }
            }],
            series: [{
                name: '2016',
                type: 'bar',
                data: [],
                barWidth: 10, //柱子宽度
                barGap: 1, //柱子之间间距
                itemStyle: {
                    normal: {
                        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                            offset: 0,
                            color: '#008cff'
                        }, {
                            offset: 1,
                            color: '#005193'
                        }]),
                        opacity: 1,
                    }
                }
            }, {
                name: '2017',
                type: 'bar',
                data: [],
                barWidth: 10,
                barGap: 1,
                itemStyle: {
                    normal: {
                        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                            offset: 0,
                            color: '#00da9c'
                        }, {
                            offset: 1,
                            color: '#007a55'
                        }]),
                        opacity: 1,
                    }
                }
            }, {
                name: '2018',
                type: 'bar',
                data: [],
                barWidth: 10,
                barGap: 1,
                itemStyle: {
                    normal: {
                        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                            offset: 0,
                            color: '#c4e300'
                        }, {
                            offset: 1,
                            color: '#728400'
                        }]),
                        opacity: 1,
                    }
                }
            }]
        };

        var jsondata = JSON.parse(json.name);
        $.each(jsondata, function (index, item) {
            for (var i = 0; i < option.xAxis.length; i++) {
                option.xAxis[i].data.push(item.village);
            }
        });

        var jsonname = JSON.parse(json.data);
        $.each(jsonname, function (index, item) {
            for (var i = 0; i < option.series.length; i++) {
                if (item.years == option.series[i].name) {
                    option.series[i].data.push(item.yield);
                }

            }
        });
        myChart.setOption(option);
    },
    error: function (errorMsg) {
        alert("请求数据失败");
    }
});
